package org.youtu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.youtu.common.enums.UtTsType;
import org.youtu.dao.BaseMapper;
import org.youtu.dao.ChatGroupMapper;
import org.youtu.dao.ThingMapper;
import org.youtu.model.ChatGroup;
import org.youtu.model.Help;
import org.youtu.model.Thing;
import org.youtu.model.ThingStory;
import org.youtu.model.User;

@Service("chatGroupService")
public class ChatGroupService extends BaseService<ChatGroup> {

	@Autowired
	private ChatGroupMapper BaseMapper;

	@Override
	public BaseMapper<ChatGroup> getBaseMapper() {
		// TODO Auto-generated method stub
		return this.BaseMapper;
	}

	/**
	 * 寻找一个聊天组，没有就新建一个返回
	 * 
	 * @param thing
	 * @param user
	 * @param utTsType
	 * @return
	 */
	public ChatGroup getChatGroup(Thing thing, ThingStory thingStory, Help help, User user, UtTsType utTsType) {

		ChatGroup cg = null;
		switch (utTsType) {
		case UserThing:
			cg = this.BaseMapper.selectByUserIdAndPrimaryKey(thing.getfId(),user.getfId());
			if (cg == null) {
				cg = new ChatGroup();
				cg.setfThingId(thing.getfId());
				cg.setfUserId(user.getfId());
				cg.setfType(utTsType.ordinal());
				this.BaseMapper.insert(cg);
				return cg;
			}
			break;
		case ThingStory:
			cg = this.BaseMapper.selectByUserIdAndPrimaryKey(thingStory.getfId(),user.getfId());
			if (cg == null) {
				cg = new ChatGroup();
				cg.setfThingId(thingStory.getfId());
				cg.setfUserId(user.getfId());
				cg.setfType(utTsType.ordinal());
				this.BaseMapper.insert(cg);
				return cg;
			}
			break;
		case Help:
			cg = this.BaseMapper.selectByUserIdAndPrimaryKey(help.getfId(),user.getfId());
			if (cg == null) {
				cg = new ChatGroup();
				cg.setfThingId(help.getfId());
				cg.setfUserId(user.getfId());
				cg.setfType(utTsType.ordinal());
				this.BaseMapper.insert(cg);
				return cg;
			}
			break;
		}
		return cg;
	}

	public List<ChatGroup> getListByThing(Thing thing) {
		ChatGroup cg = new ChatGroup();
		cg.setfThingId(thing.getfId());
		return this.BaseMapper.list(cg);
	}

}
